package com.lzx.controller;

import com.alibaba.fastjson.JSON;
import com.lzx.model.User;
import com.lzx.model.index.GrainSales;
import com.lzx.model.index.GrainYield;
import com.lzx.model.index.PlantArea;
import com.lzx.model.index.PlantType;
import com.lzx.redis.RedisService;
import com.lzx.service.IndexService;
import com.lzx.utils.JsonResultUtil;
import com.lzx.utils.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.List;

@RestController
@RequestMapping("/index")
public class IndexController {
    @Autowired
    IndexService indexService;
    @Autowired
    RedisService redisService;

    @RequestMapping("/index")
    public ModelAndView index(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView("system/index");
        User user = SessionUtil.getUser(request);
        System.out.println("#######"+user.getUsername());
        mv.addObject("user", user);
        return mv;
    }

    @RequestMapping("/liuxiangtu")
    public ModelAndView test(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView("system/liuxiangtu");
        return mv;
    }

    @RequestMapping("/plantArea")
    public Object plantArea(HttpServletRequest request) {
        List<PlantArea> planeAreaList = null;
        try {
            planeAreaList = indexService.findPlaneArea();
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResultUtil.getErrorJson("查询作物面积失败");
        }
       /* Object planeAreaListredis = redisService.get("planeAreaList");
        if (planeAreaListredis == null) {
            redisService.set("planeAreaList", JSON.toJSONString(planeAreaList), 120000L);
            System.out.println("tomcat");
            System.out.println(JSON.toJSONString(planeAreaList));
            return JSON.toJSONString(planeAreaList);
        } else {
            System.out.println("redis");
            System.out.println(JSON.toJSONString(planeAreaList));
            return planeAreaListredis;
        }*/
        return JSON.toJSONString(planeAreaList);
    }

    @RequestMapping("/plantType")
    public Object plantType(HttpServletRequest request) {
        List<PlantType> plantType = null;
        try {
            plantType = indexService.findPlantType();
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResultUtil.getErrorJson("查询作物面积失败");
        }
        return JSON.toJSONString(plantType);
    }

    @RequestMapping("/GrainYield")
    public Object GrainYield(HttpServletRequest request) {
        List<GrainYield> GrainYield = null;
        try {
            GrainYield = indexService.findGrainYield();
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResultUtil.getErrorJson("查询作物面积失败");
        }
        return JSON.toJSONString(GrainYield);
    }

    @RequestMapping("/GrainSales")
    public Object GrainSales(HttpServletRequest request) {
        List<GrainSales> GrainSales = null;
        try {
            GrainSales = indexService.findGrainSales();
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResultUtil.getErrorJson("查询作物面积失败");
        }
        return JSON.toJSONString(GrainSales);
    }

}
